package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.ServiceInfoVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：农事服务详情
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/25 14:44
 * 修改备注：
 * ================================================
 */

public class AgriculturalServiceDetailActivity extends BaseActivity {

    @Bind(R.id.activity_agricultural_service_details_wv)
    WebView webView;
    @Bind(R.id.item_agricultural_service_tv_title)
    TextView itemAgriculturalServiceTvTitle;
    @Bind(R.id.item_agricultural_service_tv_name)
    TextView itemAgriculturalServiceTvName;
    @Bind(R.id.item_agricultural_service_tv_date)
    TextView itemAgriculturalServiceTvDate;
    @Bind(R.id.item_agricultural_service_tv_praise)
    TextView itemAgriculturalServiceTvPraise;
    @Bind(R.id.item_agricultural_service_iv_praise)
    ImageView itemAgriculturalServiceIvPraise;
    @Bind(R.id.item_agricultural_service_tv_pageView)
    TextView itemAgriculturalServiceTvPageView;
    @Bind(R.id.agricultural_service_head)
    RelativeLayout agriculturalServiceHead;

    private ServiceInfoVO serviceInfoVO;
    private String summaryUrl;
    private String shareUrl;
    private String title;
    private String content;
    private String imgUrl = "";

    private boolean isPraise = false;


    @Override
    public void setLayout() {
        setTitle("农事服务详情");
        setContentLayout(R.layout.activity_agricultural_service_detail);
        setRightImage(R.drawable.ic_service_share);
    }

    @Override
    public void initMember() {

        //启用支持javascript
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        if (getIntent().hasExtra("toServiceDetails")) {
            mIvRight.setVisibility(View.VISIBLE);
            serviceInfoVO = (ServiceInfoVO) getIntent().getSerializableExtra("toServiceDetails");
            summaryUrl = URL.SERVER + serviceInfoVO.getH5Url().substring(1);
            shareUrl = URL.SERVER + "admin/agrisermgr/infoH5ShareContent?id=" + serviceInfoVO.getId();
            title = serviceInfoVO.getTitle();
            imgUrl = URL.URL_SHOW_IMAGE + serviceInfoVO.getImg() + Constant.SHOW_IMAGE_TYPE_SMALL;

            itemAgriculturalServiceTvTitle.setText(title);
            itemAgriculturalServiceTvName.setText(serviceInfoVO.getAuthor());
            itemAgriculturalServiceTvDate.setText(serviceInfoVO.getPublishDate());
            itemAgriculturalServiceTvPraise.setText(serviceInfoVO.getPraiseNum() + "赞同");
            itemAgriculturalServiceTvPageView.setText(serviceInfoVO.getPageView() + "浏览");

            content = "";

            if (!StringUtil.isStrEmpty(serviceInfoVO.getContent())) {
                content = StringUtil.stripHtml(serviceInfoVO.getContent());
                if (content.length() > 30)
                    content = StringUtil.stripHtml(serviceInfoVO.getContent()).substring(0, 30);
            } else
                content = "点击查看详情";


            if (summaryUrl != null) {
                webView.loadUrl(summaryUrl);
            }

            // 浏览次数统计
            BusinessAccount.appBrowse(this, serviceInfoVO.getId(), "4", title, summaryUrl, mHandler);
        } else if (getIntent().hasExtra("JPush_service")) {//由通知栏进入
            mIvRight.setVisibility(View.INVISIBLE);
            summaryUrl = (String) getIntent().getSerializableExtra("JPush_service");
            summaryUrl = URL.SERVER + summaryUrl.substring(1).replace("infoH5Content", "infoH5ShareContent");
            agriculturalServiceHead.setVisibility(View.GONE);
            webView.loadUrl(summaryUrl);
        } else if (getIntent().hasExtra("bannerId")) { // 由首页轮播图进入
            mIvRight.setVisibility(View.INVISIBLE);
            String id = (String) getIntent().getSerializableExtra("bannerId");
            summaryUrl = URL.SERVER + "admin/agrisermgr/infoH5ShareContent?id=" + id;
            agriculturalServiceHead.setVisibility(View.GONE);
            webView.loadUrl(summaryUrl);
        }
    }

    @Override
    public void addListener() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PRAISE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PRAISE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        String s = FastJsonUtil.getString(object.toString(), "totalNum");
        itemAgriculturalServiceTvPraise.setText(s + "赞同");
        itemAgriculturalServiceIvPraise.setImageResource(R.drawable.icon_praise);
        isPraise = true;
    }

    @Override
    public void rightOnClick() {
         }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 返回
     */
    public void back() {
        Utils.finishActivity(AgriculturalServiceDetailActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.item_agricultural_service_tv_date, R.id.item_agricultural_service_layout_praise})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.item_agricultural_service_tv_date:
                break;
            case R.id.item_agricultural_service_layout_praise: // 赞同
                if (serviceInfoVO != null) {
                    if (isPraise)
                        showToast("您已经赞过啦！");
                    else
                        BusinessAccount.appPraise(AgriculturalServiceDetailActivity.this, serviceInfoVO.getId(), "4", title, summaryUrl, mHandler);
                }
                break;
        }
    }
}
